package com.exposit.zbs.utils;

import org.dozer.CustomFieldMapper;
import org.dozer.classmap.ClassMap;
import org.dozer.fieldmap.FieldMap;
import org.hibernate.collection.internal.PersistentSet;


/**
 * Created by Andrey on 17.08.2014.
 */
public class DozerCustomMapper implements CustomFieldMapper {

	@Override
	public boolean mapField(Object source, Object destination, Object sourceFieldValue, ClassMap classMap, FieldMap fieldMapping) {
		// Check if field is a Hibernate PersistentSet
		if (!(sourceFieldValue instanceof PersistentSet)) {
			// Allow dozer to map as normal
			return false;
		}

		// Check if field is already initialized
		if (((PersistentSet) sourceFieldValue).wasInitialized()) {
			// Allow dozer to map as normal
			return false;
		}

		// Set destination to null, and tell dozer that the field is mapped
		destination = null;
		return true;
	}
}
